package com.yanftch.stopcoding.request;

import com.alibaba.fastjson2.JSONObject;
import com.yanftch.stopcoding.bean.HotBean;
import com.yanftch.stopcoding.bean.TextBean;
import com.yanftch.stopcoding.cb.RequestCallBack;
import com.yanftch.stopcoding.common.network.CallBack;
import com.yanftch.stopcoding.common.network.HttpUtils;
import com.yanftch.stopcoding.common.util.TextUtils;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class TextGaoxiao implements Request {
    private static final String url = "https://zj.v.api.aa1.cn/api/wenan-gaoxiao/?type=json";

    public interface TextCallBack {
        void onSuccess(ArrayList<HotBean> list);

        void onError(Exception e);
    }

    @Override
    public void request(RequestCallBack callBack) {
        getTextList(new TextCallBack() {
            @Override
            public void onSuccess(ArrayList<HotBean> list) {
                if (callBack != null) callBack.onSuccess(list);
            }

            @Override
            public void onError(Exception e) {
                if (callBack != null) callBack.onError(e);
            }
        });
    }

    private void getTextList(@NotNull TextCallBack zhihuHotCallBack) {
        HttpUtils.get(url, new CallBack() {
            @Override
            public void onSuccess(String response) {
                if (TextUtils.isEmpty(response)) {
                    zhihuHotCallBack.onError(new Exception("response is empty"));
                    return;
                }
                TextBean zhiHu = JSONObject.parseObject(response, TextBean.class);
                ArrayList<HotBean> list = new ArrayList<>();
                if (zhiHu != null && !TextUtils.isEmpty(zhiHu.getMsg())) {
                    String splite = "，";
                    String msg = zhiHu.getMsg();
                    if (msg.contains("。")) {
                        splite = "。";
                    } else if (msg.contains("；")) {
                        splite = "；";
                    } else if (msg.contains(";")) {
                        splite = ";";
                    } else {
                        splite = "，";
                    }
                    String[] splits = msg.split(splite);
                    if (splits != null && splits.length > 0) {
                        for (String s : splits) {
                            HotBean hotBean = new HotBean();
                            hotBean.setTitle(s);
                            list.add(hotBean);
                        }
                    }
                    zhihuHotCallBack.onSuccess(list);
                } else {
                    zhihuHotCallBack.onError(new Exception("text API路径变了，找插件开发者更新吧~"));
                }
            }

            @Override
            public void onError(Exception e) {
                zhihuHotCallBack.onError(e);
            }
        });
    }

    public static void main(String[] args) {
//        getTextList(new TextCallBack() {
//            @Override
//            public void onSuccess(ArrayList<HotBean> list) {
//                for (HotBean bean : list) {
//                    System.out.println(bean);
//                }
//            }
//
//            @Override
//            public void onError(Exception e) {
//
//            }
//        });

    }
}
